<?php
/**
* fashionelement.php
*
* Create on 2015-7-28
* Create by TT
*/

namespace haibao\app\business;
use \haibao\app\data\FashionElement as FashionElementData;
use \haibao\app\model\filter\FashionElement as FashionFilter;

class FashionElement extends \haibao\app\business\Base{
	
	/**
	 * @param \haibao\app\model\data\FashionElement $model
	 */
	public static function addModel($model){
		$data = new FashionElementData();
		$data->add($model);
	}
	
	public static function getTopTag(){
		$data = new FashionElementData();
		return $data->getTopTag();
	}
	
/**
	 * 获取标签树结构数组
	 * @param int $topId 顶级标签id
	 */
	public static function getTagTree($topId = null){
		if(!$topId){
			throw new BusinessException('顶级标签id不能为空');
		}
		$data = new FashionElementData();
		return $data->getTagTree($topId);
	}
	
	/**
	 * 根据标签id查询标签信息
	 * @param $detail 只显示基本信息
	 */
	public static function getTagById($tagId, $brief = true){
		$data = new FashionElementData();
		return $data->getTagById($tagId, $brief);
	}
	
	public static function getTagsByName($tagName,$isEnable = true){
		$data = new FashionElementData();
		$tagModels = $data->getTagsByName($tagName,$isEnable);
	
		return $tagModels;
	}
	
	/**
	 * 获取TagTree通过标签名
	 */
	public static function getTagTreeByTagName($tagName){
		$data = new FashionElementData();
		$tagInfo = self::getTagByTagName($tagName);
		if(!$tagInfo){
			return false;
		}else{
			$childrenTree = $data->getChildrenTreeByTagId($tagInfo->Id);
			$parentsTagObjArr = $data->getParentsTagArr($tagInfo->Id);
			$parentsTagArr = array();
			$parentTagTree = array();
			if($parentsTagObjArr){
				foreach($parentsTagObjArr as $key=>$tag){
					$parentsTagArr[$tag->Id] = array(
							FashionFilter::CONDITION_FIELD_NAME_ID => $tag->Id,
							FashionFilter::CONDITION_FIELD_NAME_TOPID => $tag->TopId,
							FashionFilter::CONDITION_FIELD_NAME_NAME_CN => $tag->NameCN,
							FashionFilter::CONDITION_FIELD_NAME_NAME_EN => $tag->NameEN,
							FashionFilter::CONDITION_FIELD_NAME_PARENT_ID => $tag->ParentId,
							FashionFilter::CONDITION_FIELD_NAME_STATUS=>$tag->Status
					);
				}
				unset($parentsTagArr[$tagInfo->Id]);
			}
				
			if($parentsTagArr){
				foreach($parentsTagArr as $key=>$tag){
					if($key == $tagInfo->ParentId){
						$parentsTagArr[$key]['children'] = $childrenTree;
						break;
					}
				}
			}
			$parentsTree = $data->getParentsChannelTree($parentsTagArr);
			return $parentsTree;
		}
	}
	
	/**
	 * 根据标签名查询标签信息
	 * @param string $tagName 标签名
	 * @param int $tagId 需要排除的标签id，可以为空
	 */
	public static function getTagByTagName($tagName, $tagId = null){
		$data = new FashionElementData();
		return $data->getTagByTagName($tagName, $tagId);
	}
	
	
	public static function deleteTag($tagId){
		self::checkOperation(self::FUNC_CODE_TAG_DELETE);
	
		if(!$tagId){
			throw new BusinessException("标签ID不能为空");
		}
		if(self::isExitstChildTag($tagId)){
			throw new BusinessException('当前分类下有子分类不能删除');
		}
		$data = new FashionElementData();
		$data->deleteTagById($tagId);
	
		self::saveOperateHistory(self::FUNC_CODE_TAG_DELETE, $tagId);
	}
	
	/**
	 * 当前分类下是否已经存在子分类
	 */
	private static function isExitstChildTag($tagId){
		$data = new FashionElementData();
		$count = $data->isExitstChildTag($tagId);
		return $count > 0 ? true : false;
	}
	
	public static function addTag($model, $external = false){
		self::checkOperation(self::FUNC_CODE_TAG_ADD);
	
		if(!$model->NameCN){
			throw new BusinessException('标签名不能为空');
		}
	
		$data = new FashionElementData();
		if (self::getTagByTagName($model->NameCN)){
			throw new BusinessException('标签“'.$model->NameCN.'”已存在');
		}
		$data->addTag($model);
	}
	public static function editTag($model){
		self::checkOperation(self::FUNC_CODE_TAG_EDIT);
	
		if(!$model->NameCN){
			throw new BusinessException('标签名不能为空');
		}
	
		$data = new FashionElementData();
		if (self::getTagByTagName($model->NameCN,$model->Id)){
			throw new BusinessException('标签“'.$model->NameCN.'”已存在');
		}
	
		$tagInfo = $data->getTagById($model->Id, false);
		$model->ParentId = $tagInfo->ParentId;
		$model->TopId = $tagInfo->TopId;

	
		$data->updateTagById($model);

		self::saveOperateHistory(self::FUNC_CODE_TAG_EDIT, $model->toArray(true,true));
	}
}